//
//  ChooseType.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/6/8.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//


#import "ChooseType.h"

@interface ChooseType ()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSArray *_pickerDic;
    
    
    UIView *_alphaView;
    UIView *_showView;
    
    NSString *bankNameString;
}
@property(nonatomic,strong) UIPickerView *pickerView;


@end

@implementation ChooseType

- (instancetype)initWithFrame:(CGRect)frame WithDataArray:(NSArray *)chooseTypeArray
{
    if (self = [super initWithFrame:frame] ) {
        [self loadPickerViewData];
//        _pickerDic = [NSArray array];
        _pickerDic = chooseTypeArray;
        
        [self buildView];
    }
    return self;
}


#pragma mark >>>>>>>>确定取消
- (void)selectCitySureClick:(UIButton *)sender
{
    
    if(!bankNameString.length) {
        bankNameString = _pickerDic.firstObject;
    }
    
    if ([self.delegate respondsToSelector:@selector(SelectChooseTypeViewCityStr:)]) {
        [self.delegate SelectChooseTypeViewCityStr:bankNameString];
    }
    
    [self dismissPickerView];
}


- (void)selectCityCancleClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(SelectChooseTypeViewCancle)]) {
        [self.delegate SelectChooseTypeViewCancle];
    }
}

- (void)loadPickerViewData {
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{} AndMenthodName:@"api/Hz/types" completeBlock:^(id object) {
        _pickerDic = [object[@"data"] objectForKey:@"types"];
        
    }];
}


- (void)showPickerView
{
    [_showView addSubview:_pickerView];
    
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0.35;
        _showView.y = HEIGHT/3*2;
    }];
    
    [[self windowView] addSubview:self];
}
/**
 *  消失
 */
- (void)dismissPickerView
{
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0;
        _showView.y = HEIGHT;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.pickerView removeFromSuperview];
    }];
}

- (UIView *)windowView
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    return window.subviews[0];
}


#pragma mark >>>>>>>>>>>>> pickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return _pickerDic.count;
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return [_pickerDic objectAtIndex:row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return WIDTH;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    bankNameString = [_pickerDic objectAtIndex:row];
    
}

- (void)buildView
{
    
    _showView = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT,WIDTH,HEIGHT/3)];
    [self addSubview:_showView];
    _showView.backgroundColor = [UIColor whiteColor];
    
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,5,40,30)];
    [cancleBtn setBackgroundColor:[UIColor whiteColor]];
    [_showView addSubview:cancleBtn];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = FONT(15);
    [cancleBtn addTarget:self action:@selector(selectCityCancleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH-50,5,40,30)];
    [_showView addSubview:sureBtn];
    [sureBtn setBackgroundColor:[UIColor whiteColor]];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT(15);
    [sureBtn addTarget:self action:@selector(selectCitySureClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //点击黑色背景 消失
    UIButton *disButton = [UIButton buttonWithType:UIButtonTypeCustom];
    disButton.frame = CGRectMake(0,0,WIDTH,HEIGHT/3*2);
    [self addSubview:disButton];
    [disButton addTarget:self action:@selector(selectCityCancleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(cancleBtn.frame),WIDTH,HEIGHT/3-40)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator = YES;
    
    //半透明视图
    _alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT)];
    _alphaView.backgroundColor = [UIColor blackColor];
    _alphaView.alpha = 0;
    [[self windowView] addSubview:_alphaView];
    
    
}


@end


